package com.java110.mall.cart.smo.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountDto;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductGroupOrderDto;
import com.java110.dto.product.ProductSeckillDto;
import com.java110.dto.product.ProductSeckillOrderDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.dto.store.StoreDto;
import com.java110.dto.storeOrder.ResultComputeStoreOrderGoodsDto;
import com.java110.dto.storeOrder.ResultComputeStoreOrderPriceDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderReturnPayDto;
import com.java110.dto.storeOrder.StoreOrderIntegralDto;
import com.java110.dto.storeOrder.StoreOrderLedgerAcctDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.cart.*;
import com.java110.intf.goods.*;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.cart.bmo.storeOrder.IUpdateStoreOrderBMO;
import com.java110.mall.cart.storeOrderEngine.StoreGroupOrderEngine;
import com.java110.mall.cart.storeOrderEngine.StoreOrderEngine;
import com.java110.po.product.ProductSeckillOrderPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.po.storeOrder.StoreOrderIntegralPo;
import com.java110.po.storeOrder.StoreOrderLedgerAcctPo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UnifiedOrderInnerServiceSMOImpl implements IUnifiedOrderInnerServiceSMO {

    @Autowired
    private StoreOrderEngine storeOrderEngine;

    @Autowired
    private IUpdateStoreOrderBMO updateStoreOrderBMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;


    public static final int USER_FLAG_NOBODY = 1; // 游客
    public static final int USER_FLAG_VIP = 2; // vip
    public static final int USER_FLAG_OWNER = 3; // 业主
    public static final int USER_FLAG_VIP_OWNER = 4; //业主也是vip


    @Autowired
    private IStoreCartInnerServiceSMO storeCartInnerServiceSMOImpl;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userInnerServiceSMOImpl;


    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;

    @Autowired
    private IDistributionModeInnerServiceSMO distributionModeInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IProductGroupOrderV1InnerServiceSMO productGroupOrderV1InnerServiceSMOImpl;

    @Autowired
    private StoreGroupOrderEngine storeGroupOrderEngine;

    @Autowired
    private IProductSeckillV1InnerServiceSMO productSeckillV1InnerServiceSMOImpl;

    @Autowired
    private IProductSeckillOrderV1InnerServiceSMO productSeckillOrderV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderIntegralV1InnerServiceSMO storeOrderIntegralV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderLedgerAcctV1InnerServiceSMO storeOrderLedgerAcctV1InnerServiceSMOImpl;

    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;

    @Override
    public StoreOrderReturnPayDto unifiedOrder(@RequestBody JSONObject reqJson) {
        StoreOrderReturnPayDto storeOrderReturnPayDto = storeOrderEngine.unifiedOrder(reqJson);
        return storeOrderReturnPayDto;
    }

    @Override
    public ResponseEntity<String> payNotifyOrder(@RequestBody JSONObject reqJson) {

        StoreOrderPo storeOrderPo = BeanConvertUtil.covertBean(reqJson, StoreOrderPo.class);
        ResponseEntity<String> responseEntity = updateStoreOrderBMOImpl.update(storeOrderPo);
        if (!reqJson.containsKey("sendIntegral")) {
            return responseEntity;
        }

        StoreOrderDto storeOrderDto = new StoreOrderDto();
        storeOrderDto.setOrderId(reqJson.getString("orderId"));
        List<StoreOrderDto> storeOrderDtos = storeOrderInnerServiceSMOImpl.queryStoreOrders(storeOrderDto);

        if (ListUtil.isNull(storeOrderDtos)) {
            return responseEntity;
        }

        StoreOrderIntegralPo storeOrderIntegralPo = new StoreOrderIntegralPo();
        storeOrderIntegralPo.setSoiId(GenerateCodeFactory.getGeneratorId("11"));
        storeOrderIntegralPo.setPersonName(storeOrderDtos.get(0).getPersonName());
        storeOrderIntegralPo.setAmount(reqJson.getString("sendIntegral"));
        storeOrderIntegralPo.setMoney(reqJson.getString("sendIntegralMoney"));
        storeOrderIntegralPo.setOrderId(reqJson.getString("orderId"));
        storeOrderIntegralPo.setPersonId(storeOrderDtos.get(0).getPersonName());
        storeOrderIntegralPo.setRemark("购买商品赠送积分");
        storeOrderIntegralPo.setShopId(storeOrderDtos.get(0).getShopId());
        storeOrderIntegralPo.setState(StoreOrderIntegralDto.STATE_WAIT);
        storeOrderIntegralPo.setIntegralType(StoreOrderIntegralDto.TYPE_SEND);
        storeOrderIntegralV1InnerServiceSMOImpl.saveStoreOrderIntegral(storeOrderIntegralPo);

        //todo 分账表中增加积分部分的 分账金额
        StoreOrderLedgerAcctDto storeOrderLedgerAcctDto = new StoreOrderLedgerAcctDto();
        storeOrderLedgerAcctDto.setOrderId(reqJson.getString("orderId"));
        storeOrderLedgerAcctDto.setFeeSpecCd(StoreOrderLedgerAcctDto.FEE_SPEC_SERVICE_FEE);
        List<StoreOrderLedgerAcctDto> storeOrderLedgerAcctDtos
                = storeOrderLedgerAcctV1InnerServiceSMOImpl.queryStoreOrderLedgerAccts(storeOrderLedgerAcctDto);
        StoreOrderLedgerAcctPo storeOrderLedgerAcctPo = null;
        if (!ListUtil.isNull(storeOrderLedgerAcctDtos)) {
            storeOrderLedgerAcctPo = new StoreOrderLedgerAcctPo();
            storeOrderLedgerAcctPo.setLaId(storeOrderLedgerAcctDtos.get(0).getLaId());
            BigDecimal amount = new BigDecimal(storeOrderLedgerAcctDtos.get(0).getAmount());
            amount = amount.add(new BigDecimal(reqJson.getString("sendIntegralMoney"))).setScale(2, BigDecimal.ROUND_HALF_UP);
            storeOrderLedgerAcctPo.setAmount(amount.doubleValue() + "");
            return responseEntity;
        }


        AccountDto accountDto = new AccountDto();
        accountDto.setObjId(StoreDto.STORE_ADMIN);
        accountDto.setAcctType(AccountDto.ACCT_TYPE_CASH);
        List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        Assert.listOnlyOne(accountDtos, "运营账号不存在");
        storeOrderLedgerAcctPo = new StoreOrderLedgerAcctPo();
        storeOrderLedgerAcctPo.setLaId(GenerateCodeFactory.getGeneratorId("11"));
        storeOrderLedgerAcctPo.setFeeSpecCd(StoreOrderLedgerAcctDto.FEE_SPEC_SERVICE_FEE);
        storeOrderLedgerAcctPo.setAmount(reqJson.getString("sendIntegralMoney"));
        storeOrderLedgerAcctPo.setOrderId(reqJson.getString("orderId"));
        storeOrderLedgerAcctPo.setPayAcctId(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchId"));
        storeOrderLedgerAcctPo.setPayAcctName(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchName"));
        storeOrderLedgerAcctPo.setSaveType(StoreOrderLedgerAcctDto.SAVE_TYPE_ACCT);
        storeOrderLedgerAcctPo.setSaveObjId(accountDtos.get(0).getAcctId());
        storeOrderLedgerAcctPo.setShopId(storeOrderDtos.get(0).getShopId());
        storeOrderLedgerAcctPo.setStoreId(StoreDto.STORE_ADMIN);
        storeOrderLedgerAcctPo.setStoreName("运营团队");
        int flag = storeOrderLedgerAcctV1InnerServiceSMOImpl.saveStoreOrderLedgerAcct(storeOrderLedgerAcctPo);
        if (flag < 1) {
            throw new IllegalArgumentException("分账失败");
        }


        return responseEntity;
    }

    @Override
    public ResultComputeStoreOrderPriceDto computeGroupProductPrice(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "未包含商铺信息");
        Assert.hasKeyAndValue(reqJson, "groupId", "未包含拼团ID");

        ResultComputeStoreOrderPriceDto resultComputeStoreOrderPriceDto = null;
        //查询购物车 订单

        ProductDto productDto = new ProductDto();
        productDto.setShopId(reqJson.getString("shopId"));
        productDto.setProductId(reqJson.getString("productId"));
        productDto.setValueId(reqJson.getString("valueId"));
        productDto.setGroupId(reqJson.getString("groupId"));
        List<ProductDto> productDtos = productInnerServiceSMOImpl.queryGroupProducts(productDto);

        if (ListUtil.isNull(productDtos)) {
            throw new IllegalArgumentException("未包含商品");
        }

        //查询配送方式
        DistributionModeDto distributionModeDto = getShopDistributionMode(reqJson.getString("shopId"));
        String sendWay = DistributionModeDto.MODE_TYPE_USER;
        double sendFee = 0;
        if (distributionModeDto != null) {
            sendWay = distributionModeDto.getModeType();
            sendFee = StringUtil.isNumber(distributionModeDto.getFixedFee()) ? Double.parseDouble(distributionModeDto.getFixedFee()) : 0;
        }

        // 查询用户角色
        List<ResultComputeStoreOrderGoodsDto> goodsDtos = new ArrayList<>();
        ResultComputeStoreOrderGoodsDto goodsDto = null;
        BigDecimal totalPayPriceDec = BigDecimal.valueOf(0.0);
        if (DistributionModeDto.MODE_TYPE_STORE.equals(sendWay)) {
            totalPayPriceDec = BigDecimal.valueOf(sendFee);
        }
        BigDecimal deductionDec = BigDecimal.valueOf(0.0);
        ProductDto tmpProductDto = productDtos.get(0);

        goodsDto = BeanConvertUtil.covertBean(tmpProductDto, ResultComputeStoreOrderGoodsDto.class);
        goodsDto.setGoodsNum(1.0);
        if (reqJson.containsKey("goodsNum")) {
            goodsDto.setGoodsNum(reqJson.getDouble("goodsNum"));
        }

        goodsDto.setPayPrice(getPayPrice(tmpProductDto, goodsDto));

        goodsDto.setExtProdId(tmpProductDto.getExtProdId());
        goodsDto.setExtValueId(tmpProductDto.getExtValueId());
        goodsDto.setCsId(tmpProductDto.getCsId());
        goodsDto.setSuType(tmpProductDto.getSuType());
        String payPriceName = "拼团价";
        goodsDto.setPayPriceName(payPriceName);

        totalPayPriceDec = totalPayPriceDec.add(BigDecimal.valueOf(goodsDto.getPayPrice()));

        deductionDec = getDeductionDec(deductionDec, tmpProductDto, goodsDto.getGoodsNum());

        goodsDtos.add(goodsDto);


        double totalPayPrice = totalPayPriceDec.setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();

        ProductGroupOrderDto productGroupOrderDto = new ProductGroupOrderDto();
        productGroupOrderDto.setGroupId(reqJson.getString("groupId"));
        productGroupOrderDto.setState(ProductGroupOrderDto.STATE_W);
        int usedCount = productGroupOrderV1InnerServiceSMOImpl.queryProductGroupOrdersCount(productGroupOrderDto);
        int userCount = Integer.parseInt(goodsDto.getUserCount());

        if (usedCount > userCount) {
            usedCount = 0;
        } else {
            usedCount = userCount - usedCount;
        }
        goodsDto.setNoBuyerCount(usedCount + "");

        List<String> productIds = new ArrayList<>();
        productIds.add(tmpProductDto.getProductId());

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (FileRelDto tmpFileRelDto : fileRelDtos) {
            if (!goodsDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                continue;
            }

            if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                continue;
            }
            url = imgUrl + tmpFileRelDto.getFileRealName();

            if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                url = tmpFileRelDto.getFileRealName();

            }


            goodsDto.setCoverPhoto(url);
        }


        resultComputeStoreOrderPriceDto = new ResultComputeStoreOrderPriceDto(totalPayPrice, sendWay, sendFee, deductionDec.doubleValue(), goodsDtos);
        return resultComputeStoreOrderPriceDto;

    }

    /**
     * 秒杀算价
     *
     * @param reqJson
     * @return
     */
    @Override
    public ResultComputeStoreOrderPriceDto computeSkillProductPrice(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含商铺信息");
        Assert.hasKeyAndValue(reqJson, "killId", "未包含秒杀ID");
        Assert.hasKeyAndValue(reqJson, "goodsNum", "未包含数量");

        ResultComputeStoreOrderPriceDto resultComputeStoreOrderPriceDto = null;
        //查询购物车 订单

        ProductDto productDto = new ProductDto();
        productDto.setShopId(reqJson.getString("shopId"));
        productDto.setProductId(reqJson.getString("productId"));
        productDto.setValueId(reqJson.getString("valueId"));
        productDto.setKillId(reqJson.getString("killId"));
        List<ProductDto> productDtos = productInnerServiceSMOImpl.querySeckillProducts(productDto);

        if (ListUtil.isNull(productDtos)) {
            throw new IllegalArgumentException("未包含商品");
        }

        //查询配送方式
        DistributionModeDto distributionModeDto = getShopDistributionMode(reqJson.getString("shopId"));
        String sendWay = DistributionModeDto.MODE_TYPE_USER;
        double sendFee = 0;
        if (distributionModeDto != null) {
            sendWay = distributionModeDto.getModeType();
            sendFee = StringUtil.isNumber(distributionModeDto.getFixedFee()) ? Double.parseDouble(distributionModeDto.getFixedFee()) : 0;
        }

        // 查询用户角色
        List<ResultComputeStoreOrderGoodsDto> goodsDtos = new ArrayList<>();
        ResultComputeStoreOrderGoodsDto goodsDto = null;
        BigDecimal totalPayPriceDec = BigDecimal.valueOf(0.0);
        if (DistributionModeDto.MODE_TYPE_STORE.equals(sendWay)) {
            totalPayPriceDec = BigDecimal.valueOf(sendFee);
        }
        BigDecimal deductionDec = BigDecimal.valueOf(0.0);
        ProductDto tmpProductDto = productDtos.get(0);

        goodsDto = BeanConvertUtil.covertBean(tmpProductDto, ResultComputeStoreOrderGoodsDto.class);
        goodsDto.setGoodsNum(1.0);
        if (reqJson.containsKey("goodsNum")) {
            goodsDto.setGoodsNum(reqJson.getDouble("goodsNum"));
        }

        goodsDto.setPayPrice(getSkillPayPrice(tmpProductDto, goodsDto));

        goodsDto.setExtProdId(tmpProductDto.getExtProdId());
        goodsDto.setExtValueId(tmpProductDto.getExtValueId());
        goodsDto.setCsId(tmpProductDto.getCsId());
        goodsDto.setSuType(tmpProductDto.getSuType());
        String payPriceName = "秒杀价";
        goodsDto.setPayPriceName(payPriceName);

        totalPayPriceDec = totalPayPriceDec.add(BigDecimal.valueOf(goodsDto.getPayPrice()));

        deductionDec = getDeductionDec(deductionDec, tmpProductDto, goodsDto.getGoodsNum());

        //todo 计算库存
        ProductSeckillDto productSeckillDto = new ProductSeckillDto();
        productSeckillDto.setKillId(reqJson.getString("killId"));
        productSeckillDto.setShopId(reqJson.getString("shopId"));
        List<ProductSeckillDto> productSeckillDtos = productSeckillV1InnerServiceSMOImpl.queryProductSeckills(productSeckillDto);
        goodsDto.setKillStock(0);
        if (!ListUtil.isNull(productSeckillDtos)) {
            goodsDto.setKillStock(Long.parseLong(productSeckillDtos.get(0).getKillStock()));
        }

        goodsDtos.add(goodsDto);


        double totalPayPrice = totalPayPriceDec.setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();


        List<String> productIds = new ArrayList<>();
        productIds.add(tmpProductDto.getProductId());

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (FileRelDto tmpFileRelDto : fileRelDtos) {
            if (!goodsDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                continue;
            }

            if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                continue;
            }
            url = imgUrl + tmpFileRelDto.getFileRealName();

            if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                url = tmpFileRelDto.getFileRealName();

            }

            goodsDto.setCoverPhoto(url);
        }

        resultComputeStoreOrderPriceDto = new ResultComputeStoreOrderPriceDto(totalPayPrice, sendWay, sendFee, deductionDec.doubleValue(), goodsDtos);

        return resultComputeStoreOrderPriceDto;
    }

    @Override
    public ResultVo unifiedKillProductOrder(JSONObject reqJson) {

        ProductDto productDto = new ProductDto();
        productDto.setShopId(reqJson.getString("shopId"));
        productDto.setProductId(reqJson.getString("productId"));
        productDto.setValueId(reqJson.getString("valueId"));
        productDto.setKillId(reqJson.getString("killId"));
        List<ProductDto> productDtos = productInnerServiceSMOImpl.querySeckillProducts(productDto);

        if (ListUtil.isNull(productDtos)) {
            throw new IllegalArgumentException("未包含商品");
        }

        ProductSeckillOrderDto productSeckillOrderDto = new ProductSeckillOrderDto();
        productSeckillOrderDto.setOrderId(reqJson.getString("orderId"));
        productSeckillOrderDto.setState(ProductSeckillOrderDto.STATE_WAIT);
        productSeckillOrderDto.setShopId(reqJson.getString("shopId"));
        List<ProductSeckillOrderDto> productSeckillOrderDtos = productSeckillOrderV1InnerServiceSMOImpl.queryProductSeckillOrders(productSeckillOrderDto);

        if (ListUtil.isNull(productSeckillOrderDtos)) {
            throw new CmdException("订单已经提交");
        }

        JSONObject orderInfo = new JSONObject();
        orderInfo.put("shopId", productSeckillOrderDtos.get(0).getShopId());
        orderInfo.put("addressId", productSeckillOrderDtos.get(0).getAddressId());
        orderInfo.put("appId", reqJson.getString("app-id"));

        JSONObject storeOrder = new JSONObject();
        storeOrder.put("personId", productSeckillOrderDtos.get(0).getBuyerId());
        storeOrder.put("personName", productSeckillOrderDtos.get(0).getBuyerName());
        storeOrder.put("userId", productSeckillOrderDtos.get(0).getBuyerId());
        storeOrder.put("remark", productSeckillOrderDtos.get(0).getRemark());
        orderInfo.put("storeOrder", storeOrder);

        JSONArray storeOrderCarts = new JSONArray();

        JSONObject storeOrderCart = new JSONObject();
        storeOrderCart.put("cateType", "1");
        storeOrderCart.put("productId", productDtos.get(0).getProductId());
        storeOrderCart.put("prodName", productDtos.get(0).getProdName());
        storeOrderCart.put("valueId", productDtos.get(0).getValueId());
        storeOrderCart.put("goodsNum", productSeckillOrderDtos.get(0).getGoodsNum());
        storeOrderCart.put("shopId", productSeckillOrderDtos.get(0).getShopId());
        storeOrderCart.put("business", StoreOrderDto.BUSINESS_KILL);
        storeOrderCart.put("killId", reqJson.getString("killId"));

        storeOrderCart.put("cartType", "3306");
        storeOrderCarts.add(storeOrderCart);
        orderInfo.put("storeOrderCarts", storeOrderCarts);
        StoreOrderReturnPayDto storeOrderReturnPayDto = storeOrderEngine.unifiedOrder(orderInfo);

        StoreOrderPo storeOrderPo = new StoreOrderPo();
        storeOrderPo.setOrderId(storeOrderReturnPayDto.getOrderId());
        updateStoreOrderBMOImpl.update(storeOrderPo);

        ProductSeckillOrderPo productSeckillOrderPo = new ProductSeckillOrderPo();
        productSeckillOrderPo.setOrderId(reqJson.getString("orderId"));
        productSeckillOrderPo.setState(ProductSeckillOrderDto.STATE_COMPLETE);
        productSeckillOrderPo.setCartId(storeOrderReturnPayDto.getOrderId());
        int flag = productSeckillOrderV1InnerServiceSMOImpl.updateProductSeckillOrder(productSeckillOrderPo);
        if (flag < 1) {
            throw new CmdException("下单失败");
        }
        return new ResultVo(ResultVo.CODE_OK, ResultVo.MSG_OK);
    }


    @Override
    public ResultVo unifiedGroupProductOrder(@RequestBody JSONObject reqJson) {

        return storeGroupOrderEngine.unifiedOrder(reqJson);
    }


    private BigDecimal getDeductionDec(BigDecimal deductionDec, ProductDto tmpProductDto, double goodsNum) {
        BigDecimal goodsDeductionDec = new BigDecimal(tmpProductDto.getDefaultSpecValue().getDeduction());
        goodsDeductionDec = goodsDeductionDec.multiply(BigDecimal.valueOf(goodsNum)).setScale(2, BigDecimal.ROUND_HALF_UP);
        deductionDec = deductionDec.add(goodsDeductionDec);
        return deductionDec;
    }

    private DistributionModeDto getShopDistributionMode(String shopId) {
        DistributionModeDto distributionModeDto = new DistributionModeDto();
        distributionModeDto.setShopId(shopId);
        List<DistributionModeDto> shopDistributionModeDtos = distributionModeInnerServiceSMOImpl.queryDistributionModes(distributionModeDto);
        if (ListUtil.isNull(shopDistributionModeDtos)) {
            return null;
        }
        return shopDistributionModeDtos.get(0);
    }

    /**
     * 计算 支付价格
     *
     * @param tmpProductDto
     * @return
     */
    private double getPayPrice(ProductDto tmpProductDto, ResultComputeStoreOrderGoodsDto goodsDto) {

        double payPrice = Double.parseDouble(tmpProductDto.getGroupPrice());

        payPrice = BigDecimal.valueOf(goodsDto.getGoodsNum()).multiply(BigDecimal.valueOf(payPrice)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();

        return payPrice;
    }

    private double getSkillPayPrice(ProductDto tmpProductDto, ResultComputeStoreOrderGoodsDto goodsDto) {

        double payPrice = Double.parseDouble(tmpProductDto.getKillPrice());

        payPrice = BigDecimal.valueOf(goodsDto.getGoodsNum()).multiply(BigDecimal.valueOf(payPrice)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();

        return payPrice;
    }


    public int getUserFlag(ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
        //查询用户信息
        int userFlag = USER_FLAG_NOBODY;
        UserDto userDto = new UserDto();
        userDto.setUserId(cmdDataFlowContext.getReqHeaders().get("user-id"));
        userDto.setPage(1);
        userDto.setRow(1);
        List<UserDto> userDtos = userInnerServiceSMOImpl.queryUsers(userDto);
        if (userDtos == null || userDtos.size() < 1) {
            return userFlag;
        }

        String tel = userDtos.get(0).getTel();

        //
//        OwnerDto ownerDto = new OwnerDto();
//        ownerDto.setLink(tel);
//        int ownerCount = ownerInnerServiceSMOImpl.queryOwnersCount(ownerDto);
//        if (ownerCount > 0) {
//            userFlag = USER_FLAG_OWNER;
//        }

        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(reqJson.getString("shopId"));
        shopVipUserDto.setVipTel(tel);
        shopVipUserDto.setPage(1);
        shopVipUserDto.setRow(1);
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        if (shopVipUserDtos != null && shopVipUserDtos.size() > 0) {
            if (userFlag == USER_FLAG_OWNER) {
                userFlag = USER_FLAG_VIP_OWNER;
            } else {
                userFlag = USER_FLAG_VIP;
            }
            reqJson.put("vipUserDto", shopVipUserDtos.get(0));
        }

        return userFlag;

    }
}
